    program episode_rets_daily_wt_rets
    character*6 ticker(5000)
    character*8 cusp(5000)
    character*9 temp9
    integer(kind=8) nshrs(5000),nvalue(5000),jvalue(5000),ivol(5000),numshrs(5000)
    dimension jast(5000),jdate(5000),itickt(5000),age(5000),favg(5000),fmax(5000),fmin(5000),fstd(5000),iperm(5000)
    dimension  price(5000),ishrout(5000),vwret(5000),util(5000),retrn(5000),dfee(5000),nep(5000)
!      open(unit=10,file='e:\FIS_duration\episode_data.txt')
!      open(unit=99,file='e:\FIS_duration\rets_daily_weighted_returns.txt')
      open(unit=10,file='e:\FIS_duration\episode_data_rep.txt')
      open(unit=99,file='e:\FIS_duration\rets_daily_weighted_returns_rep.txt')
      iend=0
      i=0
1     i=i+1      
      read(10,1000,end=900)nep(i),jast(i),jdate(i),cusp(i),ticker(i),itickt(i),numshrs(i),age(i),favg(i),fmax(i),fmin(i),fstd(i),iperm(i),price(i),ivol(i),retrn(i),ishrout(i),vwret(i),util(i),nshrs(i)     
1000  format(1x,i4,2(1x,i8),1x,a8,2x,a6,4x,i9,1x,i14,5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f10.6,1x,i9,1x,f9.6,1x,f6.2,1x,i13) 
      if(nep(i).eq.nep(1)) go to 1
5     icnt=i-1
      if(icnt.lt.3) go to 500
      icntm=icnt-1
      totsz=0.
      do 20 ii=2,icntm
20         totsz=totsz+float(numshrs(ii))/float(ishrout(ii))
      days=float(icntm)-1.
      avgsz=totsz/days
      tret=1.
      xsret=1.
      dwtret=0.
      dwtxsrt=0.
      dret=0.
      dxsret=0.
      cdwtret=0.
      cdwtxsrt=0.
      cdret=0.
      cdxsret=0.
      do 40 ii=3,icnt
          im=ii-1
          rat=float(numshrs(im))/float(ishrout(im))
          tret=tret*(1.+retrn(ii))
          xsret=xsret*(1.+retrn(ii)-vwret(ii))
          dwtret=dwtret+retrn(ii)*(rat/totsz)
          dwtxsrt=dwtxsrt+(retrn(ii)-vwret(ii))*(rat/totsz)
          dret=dret+retrn(ii)/days
          dxsret=dxsret+(retrn(ii)-vwret(ii))/days
          cdwtret=cdwtret+log(1.+retrn(ii))*(rat/totsz)
          cdwtxsrt=cdwtxsrt+log(1.+retrn(ii)-vwret(ii))*(rat/totsz)
          cdret=cdret+log(1.+retrn(ii))/days
          cdxsret=cdxsret+log(1.+retrn(ii)-vwret(ii))/days
40    continue
      tret=tret-1.
      xsret=xsret-1.
      wtret=wtret-1.
      wtxsrt=wtxsrt-1.
      itype=2
      if(favg(icnt).lt.2.and.favg(icntm).lt.2) itype=1
      if(jdate(icnt).eq.20231229) itype=3
      write(6,6000)nep(1),itype,icnt,tret,xsret,dwtret,dwtxsrt,dret,dxsret,cdwtret,cdwtxsrt,cdret,cdxsret
      write(99,6000)nep(1),itype,icnt,tret,xsret,dwtret,dwtxsrt,dret,dxsret,cdwtret,cdwtxsrt,cdret,cdxsret
6000  format(1x,i4,1x,i1,1x,i4,10(1x,f10.6))      
500   jast(1)=jast(i)
      jdate(1)=jdate(i)
      cusp(1)=cusp(i)
      ticker(1)=ticker(i)
      itickt(1)=itickt(i)
      numshrs(1)=numshrs(i)
      jvalue(1)=jvalue(i)
      age(1)=age(i)
      favg(1)=favg(i)
      fmax(1)=fmax(i)
      fmin(1)=fmin(i)
      fstd(1)=fstd(i)
      iperm(1)=iperm(i)
      price(1)=price(i)
      ivol(1)=ivol(i)
      retrn(1)=retrn(i)
      ishrout(1)=ishrout(i)
      vwret(1)=vwret(i)
      util(1)=util(i)
      nshrs(1)=nshrs(i)
      nvalue(1)=nvalue(i) 
      dfee(1)=dfee(i)
      nep(1)=nep(i)
      i=1
      go to 1
900   iend=iend+1
      if(iend.eq.1) go to 5 
      stop

    end program episode_rets_daily_wt_rets
